    //
//  ClassViewController.m
//  SmartRoadShow
//
//  Created by Tracy E on 10-9-26.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "DetailWebViewController.h"
#import "CallUtil.h"
#import  <QuartzCore/QuartzCore.h>


@implementation DetailWebViewController
@synthesize webView;

- (id)init {
    if (self = [super init]) {
		[self.view setBackgroundColor:[UIColor colorWithRed:0.79 green:0.78 blue:0.78 alpha:1.0]];
		
		webView = [[UIWebView alloc] initWithFrame:CGRectMake(5, 5, 310, 336)];
		webView.backgroundColor = [UIColor whiteColor];
		webView.layer.cornerRadius = 8;
		webView.layer.masksToBounds = YES;
		webView.dataDetectorTypes = UIDataDetectorTypeNone;
		[webView setDelegate:self];
		[self.view addSubview:webView];
		
		waitView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
		[waitView setCenter:webView.center];
		[waitView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
		[waitView setHidesWhenStopped:YES];
		[webView addSubview:waitView];
    }
    return self;
}

- (void)loadDetailInfo:(NSString *)htmlStr{
	[webView loadHTMLString:htmlStr baseURL:nil];
}

//加载web前弹出提示信息
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType{
	
	requestURLString = [[NSString stringWithFormat:@"%@",[request URL]] retain];
	
	NSLog(@"%s,%d,request : %@", __FUNCTION__, __LINE__, requestURLString);
	
	if ([requestURLString hasPrefix:@"http://maps.google.com"]) {
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您将离开该应用前往地图服务，是否继续？"
														message:nil
													   delegate:self
											  cancelButtonTitle:@"确定"
											  otherButtonTitles:@"取消",nil];
		[alert show];
		[alert release];
		
		return NO;
	}
	else if([requestURLString isEqualToString:@"tel:4008110000"]){
		CallUtil *call = [CallUtil getInstance];
		[call callPhoneNumber:@"4008110000"];
		return NO;
	}

	return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	
	if (alertView.tag == 110 && buttonIndex == 0) {
		
		NSLog(@"cancel");
		[self.navigationController popViewControllerAnimated:YES];
	}
	
	else if (buttonIndex == 0) {
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:requestURLString]];
	}
}


- (void)webViewDidStartLoad:(UIWebView *)webView{
	[waitView startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
	[waitView stopAnimating];
	
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
	[waitView stopAnimating];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"本功能需要网络支持，请检查网络连接！"
												   delegate:self
										  cancelButtonTitle:@"确定"
										  otherButtonTitles:nil];
	
	[alert setTag:110];
	[alert show];
	[alert release];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
	webView = nil;
	waitView = nil;
}


- (void)dealloc {
	[webView release];
	[waitView release];
	[requestURLString release];
    [super dealloc];

}


@end
